package cn.gov.pbc.dream.service.impl;

import java.util.Date;

import cn.gov.pbc.dream.common.ResponseCode;
import cn.gov.pbc.dream.common.ServerResponse;
import cn.gov.pbc.dream.dao.AssetPoolDoMapper;
import cn.gov.pbc.dream.dao.BankFinancingOrgDoMapper;
import cn.gov.pbc.dream.dao.BankFinancingProductInfoDoMapper;
import cn.gov.pbc.dream.pojo.dto.user.bankfinancing.org.AddOrgDto;
import cn.gov.pbc.dream.pojo.dto.user.bankfinancing.org.UpdateOrgNameDto;
import cn.gov.pbc.dream.pojo.query.bankfinancing.OrgQuery;
import cn.gov.pbc.dream.pojo.repository.BankFinancingOrgDo;
import cn.gov.pbc.dream.pojo.vo.bankfinancing.OrgVo;
import cn.gov.pbc.dream.service.IBankFinancingOrgService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("iBankFinancingOrgService")
public class BankFinancingOrgServiceImpl implements IBankFinancingOrgService {

    @Autowired
    private BankFinancingOrgDoMapper bankFinancingOrgDoMapper;

    @Autowired
    private AssetPoolDoMapper assetPoolDoMapper;

    @Autowired
    private BankFinancingProductInfoDoMapper bankFinancingProductInfoDoMapper;

    @Override
    public ServerResponse getOrgList(OrgQuery orgQuery) {
        String nameOrCode = null;
        Boolean isNoName = Boolean.FALSE;
        if (StringUtils.isNotBlank(orgQuery.getCodeOrName())) {
            nameOrCode = "%" + orgQuery.getCodeOrName() + "%";
        }
        if (orgQuery.getShowNoName() != null) {
            isNoName = orgQuery.getShowNoName();
        }
        PageHelper.startPage(orgQuery.getPageNum(), orgQuery.getPageSize());
        List<BankFinancingOrgDo> orgDoList = this.bankFinancingOrgDoMapper.selectByQuery(nameOrCode, isNoName);
        List<OrgVo> orgVoList = new ArrayList<>();
        for (BankFinancingOrgDo orgDo : orgDoList) {
            orgVoList.add(this.assembleOrgVo(orgDo));
        }
        PageInfo pageInfo = new PageInfo(orgDoList);
        pageInfo.setList(orgVoList);
        return ServerResponse.createBySuccess(pageInfo);
    }

    private OrgVo assembleOrgVo(BankFinancingOrgDo orgDo) {
        OrgVo vo = new OrgVo();
        vo.setId(orgDo.getId());
        vo.setCode(orgDo.getCode());
        vo.setName(orgDo.getName());
        return vo;
    }

    @Override
    public ServerResponse updateOrgName(UpdateOrgNameDto updateFinancingOrgNameDto) {
        if (updateFinancingOrgNameDto == null || StringUtils.isBlank(updateFinancingOrgNameDto.getOrgCode()) ||
                StringUtils.isBlank(updateFinancingOrgNameDto.getOrgName())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        int updateCount = this.bankFinancingOrgDoMapper.updateByOrgCode(updateFinancingOrgNameDto.getOrgCode(), updateFinancingOrgNameDto.getOrgName());
        if (updateCount == 0) {
            return ServerResponse.createByErrorMessage("更新失败");
        }
        return ServerResponse.createBySuccessMessage("更新成功");
    }

    @Override
    public ServerResponse deleteOrg(Integer id) {
        if (id == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        int count = this.assetPoolDoMapper.selectCountByOrg(id);
        if (count > 0) {
            return ServerResponse.createByErrorMessage("该机构有资产池数据，无法删除");
        }
        count = this.bankFinancingProductInfoDoMapper.selectCountByOrg(id);
        if (count > 0) {
            return ServerResponse.createByErrorMessage("该机构有理财产品信息数据，无法删除");
        }
        count = this.bankFinancingOrgDoMapper.deleteByPrimaryKey(id);
        if (count == 0) {
            return ServerResponse.createByErrorMessage("删除失败");
        }
        return ServerResponse.createBySuccessMessage("删除成功");
    }

    @Override
    public ServerResponse addOrg(AddOrgDto addOrgDto) {
        if (addOrgDto == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        ServerResponse response = addOrgDto.validate();
        if (!response.isSuccess()) {
            return response;
        }
        if (this.bankFinancingOrgDoMapper.selectByCode(addOrgDto.getOrgCode()) != null) {
            return ServerResponse.createByErrorMessage("机构编码已经存在，不能重复增加");
        }
        BankFinancingOrgDo insert = new BankFinancingOrgDo();
        insert.setCode(addOrgDto.getOrgCode());
        insert.setName(addOrgDto.getOrgName());
        int insertCount = this.bankFinancingOrgDoMapper.insertSelective(insert);
        if (insertCount == 0) {
            return ServerResponse.createByErrorMessage("新增机构失败");
        }
        return ServerResponse.createBySuccessMessage("新增机构成功");
    }
}
